package cn.kgc.springcloud.service.impl;

import cn.kgc.springcloud.client.CityClient;
import cn.kgc.springcloud.client.ClazzClient;
import cn.kgc.springcloud.client.GroupClient;
import cn.kgc.springcloud.client.TagClient;
import cn.kgc.springcloud.entity.*;
import cn.kgc.springcloud.mapper.StudentTagMapper;
import cn.kgc.springcloud.utils.DateUtil;
import cn.kgc.springcloud.vo.StudentVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springcloud.service.StudentService;
import cn.kgc.springcloud.mapper.StudentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mengshun
 * @description 针对表【t_student】的数据库操作Service实现
 * @createDate 2023-10-19 09:54:37
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private CityClient cityClient;

    @Resource
    private ClazzClient clazzClient;

    @Resource
    private GroupClient groupClient;

    @Resource
    private TagClient tagClient;


    @Resource
    private StudentTagMapper studentTagMapper;

    @Override
    public List<StudentVO> getList() {

        List<Student> students = studentMapper.selectList(null);

        List<StudentVO> collect = students.stream().map(student -> {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);

            // city
            Integer cityid = student.getCityid();
            City city = cityClient.getById(cityid);
            studentVO.setCity(city);

            // clazz
            Integer clazzid = student.getClazzid();
            Clazz clazz = clazzClient.getById(clazzid);
            studentVO.setClazz(clazz);

            // group
            Integer groupid = student.getGroupid();
            Group group = groupClient.getById(groupid);
            studentVO.setGroup(group);

            // tags
            Integer id = student.getId();
            // 根据学生的id  查询学生的标签id
            LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
            lambda.eq(StudentTag::getSid, id);

            // 当前学生的标签列表
            List<StudentTag> studentTags = studentTagMapper.selectList(lambda);

            List<Integer> tids = studentTags.stream().map(studentTag -> studentTag.getTid()).collect(Collectors.toList());

            List<Tag> tags = tids.stream().map(tid -> {
                // tag服务发起http请求
                Tag tag = tagClient.getById(tid);
                return tag;
            }).collect(Collectors.toList());

            studentVO.setTags(tags);

            return studentVO;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public StudentVO addStudent(StudentVO studentVO) {

        Student student = new Student();
        BeanUtils.copyProperties(studentVO, student);

        Date bir = student.getBir();
        //根据学生的生日 计算相关属性   年龄  属相  星座
        int age = DateUtil.getAge(bir);
        String attr = DateUtil.getYear(bir);
        //星座
        String star = DateUtil.getConstellation(bir);
        student.setAge(age);
        student.setStarts(star);
        student.setAttr(attr);

        // 将学生信息保存到数据库
        studentMapper.insert(student);

        Integer id = student.getId();
        List<Integer> tagIds = studentVO.getTagIds();

        tagIds.forEach(tid -> {
            StudentTag studentTag = new StudentTag();
            studentTag.setSid(id);
            studentTag.setTid(tid);
            studentTagMapper.insert(studentTag);
        });


        BeanUtils.copyProperties(student,studentVO);

        // city
        Integer cityid = student.getCityid();
        City city = cityClient.getById(cityid);
        studentVO.setCity(city);

        // clazz
        Integer clazzid = student.getClazzid();
        Clazz clazz = clazzClient.getById(clazzid);
        studentVO.setClazz(clazz);

        // group
        Integer groupid = student.getGroupid();
        Group group = groupClient.getById(groupid);
        studentVO.setGroup(group);


        // tags
        List<Tag> tags = tagIds.stream().map(tid -> {
            // tag服务发起http请求
            Tag tag = tagClient.getById(tid);
            return tag;
        }).collect(Collectors.toList());

        studentVO.setTags(tags);

        return studentVO;
    }
}




